package net.ch2.pc12.tech.ackborn.activity;

import android.R.id;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.os.Bundle;
import android.os.Handler;

import java.util.List;
import java.util.HashMap;

import net.ch2.pc12.tech.ackborn.R;
import net.ch2.pc12.tech.ackborn.activity.helper.MenuHelper;
import net.ch2.pc12.tech.ackborn.activity.thread.ThreadListThread;
import net.ch2.pc12.tech.ackborn.dto.ProgressDTO;
import net.ch2.pc12.tech.ackborn.model.BbsBoardModel;

public class BoardActivity extends ListActivity implements Runnable,OnClickListener{
    /** Called when the activity is first created. */

	SimpleAdapter mAdapter;
	private ThreadListThread thread;
	
	private String title;
	
	private final Handler handler = new Handler();

	private ProgressDTO progressDTO;
	
	TextView statusView;
	
	public ProgressDTO getProgressDTO() {
		return progressDTO;
	}

	public void setProgressDTO(ProgressDTO progressDTO) {
		this.progressDTO = progressDTO;
	}

	private String _boardId;
	private String _boardName;
	private String _boardUrl;
	
	private BbsBoardModel model;

	public BbsBoardModel getModel() {
		return model;
	}

	public void setModel(BbsBoardModel model) {
		this.model = model;
	}

	@Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);

    	Intent intent = getIntent();

    	_boardId = intent.getStringExtra("boardId");
    	_boardName = intent.getStringExtra("boardName");
    	_boardUrl = intent.getStringExtra("boardUrl");
        model = new BbsBoardModel();
        model.setBoardId(_boardId);
        model.setBoardName(_boardName);
        model.setBoardUrl(_boardUrl);

    	
    	setContentView(R.layout.board);

        ListView list = (ListView)findViewById(id.list);

        list.setOnItemClickListener(new OnItemClickListener(){

			public void onItemClick(AdapterView<?> adapter, View v, int arg2,
					long arg3) {
				TextView tv = (TextView)v.findViewById(R.id.ThreadId);
				String threadId = tv.getText().toString();
				tv = (TextView)v.findViewById(R.id.ThreadDatName);
				String datName = tv.getText().toString();
				tv = (TextView)v.findViewById(R.id.ThreadName);
				String threadName = tv.getText().toString();
				Intent intent = new Intent(getApplicationContext(),ThreadActivity.class);

				intent.putExtra("threadId", threadId);
				intent.putExtra("boardUrl", _boardUrl);
				intent.putExtra("threadName",threadName );
				intent.putExtra("datName", datName);
				intent.putExtra("boardId", _boardId);
				
				startActivity(intent);
			}
        });
        
        statusView = (TextView) findViewById(R.id.BoardStatus);
        title = _boardName;
        statusView.setText(title);

        if(model.hasCache(this)){
        	setList(false);
        }else{
        	// 確認ダイアログだして、取得するかを聞く
        	Builder builder = new Builder(this);
        	builder.setTitle(R.string.NetworkConfirm);
        	builder.setMessage(R.string.ThreadListConfirm);
        	builder.setPositiveButton(R.string.ConfirmOK, this);
        	
        	builder.setNegativeButton(R.string.ConfirmNG, this);
        	
        	builder.setCancelable(false);
        	final AlertDialog dialog = builder.create();
        	dialog.show();

        }
        
    }
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu){
		boolean result = super.onCreateOptionsMenu(menu);
		MenuHelper.setMenu(menu, MenuHelper.TYPE_THREAD_LIST);
		return result;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		switch(item.getItemId()){
		
		case MenuHelper.MENU_TO_TOP:
			toTop();
			return true;
		case MenuHelper.MENU_TO_BOTTOM:
			toBottom();
			return true;
		case MenuHelper.MENU_RELOAD:
			reload();
			return true;
		case MenuHelper.MENU_BOOKMARK:
			bookmark();
			return true;
		case MenuHelper.MENU_CONFIG:
			config();
			
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	private void toTop(){
		ListView list = (ListView)findViewById(id.list);
		list.setSelection(0);		
	}
	
	private void toBottom(){
		ListView list = (ListView)findViewById(id.list);
		list.setSelection(list.getCount());
	}
	
	private void reload(){
		setList(true);
	}
	
	private void bookmark(){
		
	}
	
	private void config(){

	}
	
	private void setList(boolean reload){
		progressDTO = new ProgressDTO();
		progressDTO.setProgress(0);
		setStatus(getString(R.string.Loading));
		thread = new ThreadListThread(handler,this,reload);
		thread.start();
	}
	
	public void run(){
		setStatus(progressDTO.getProgressMessage());
		if(progressDTO.getProgress() == ProgressDTO.COMPLETE){
			setStatus(title);
			display();
		}
	}
	
	private void display(){
		mAdapter = new SimpleAdapter(this,getList(),R.layout.threadlistrow,new String[]{"threadNum","threadName","threadCount","threadRead","threadId","datName"},new int[]{R.id.ThreadNum,R.id.ThreadName,R.id.ThreadCount,R.id.ThreadRead,R.id.ThreadId,R.id.ThreadDatName});
        setListAdapter(mAdapter);
        toTop();
	}
	
	private void setStatus(String message){
		statusView.setText(message);
	}
	
	private List<HashMap<String,String>> getList(){
		return model.getThreadList();
	}

	public void onClick(DialogInterface dialog, int which) {
		if(which == DialogInterface.BUTTON_POSITIVE){
			setList(true);
		}
		dialog.dismiss();
	}
}